package com.hunt.service.app.impl;

import com.alibaba.fastjson.JSONObject;
import com.hunt.dao.TransactionCalendarMapper;
import com.hunt.model.app.FncWarning;
import com.hunt.model.app.FncWarningResults;
import com.hunt.service.app.FncWarningService;
import com.hunt.util.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


@Service
@Transactional
public class FncWarningServiceImpl implements FncWarningService {

    private  static  SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMddhhmmss");
    private  static  SimpleDateFormat simpleDateFormat1=new SimpleDateFormat("MM-dd");
    @Autowired
    private JedisPool jedisPool;
    @Autowired
    private MongoTemplate ShapeHistoryTemplate;

    @Override
    public Result findNewFncWarning(String type,Integer position,Integer size) {
        size = position + size;
        Jedis jedis = null;
        FncWarningResults fncWarnings = new FncWarningResults();
        try {
            jedis = jedisPool.getResource();
            if (type.equals("all")){
                List<FncWarningResults> allFncResultsList = new ArrayList<>();
                Map<String, String> allFnc = jedis.hgetAll("FNC_WARRING");
                Set<Map.Entry<String, String>> allEntries = allFnc.entrySet();
                Iterator<Map.Entry<String, String>> iterator = allEntries.iterator();
                while (iterator.hasNext()){
                    Map.Entry<String, String> next = iterator.next();
                    FncWarningResults fnc = JSONObject.parseObject(next.getValue(), FncWarningResults.class);
                    allFncResultsList.add(fnc);
                }
                return Result.success(allFncResultsList);
            }else{
                List<String> result = jedis.hmget("FNC_WARRING", type);
                if (result.size() > 0 && result.get(0) != null){
                    fncWarnings = JSONObject.parseObject(result.get(0), FncWarningResults.class);
                    if (position > fncWarnings.getData().size()){
                        position = fncWarnings.getData().size();
                    }
                    if (size > fncWarnings.getData().size()){
                        size = fncWarnings.getData().size();
                    }
                    List<FncWarning> subFncWarnings = fncWarnings.getData().subList(position, size);
                    for(FncWarning f:subFncWarnings){
                        try {
                            f.setDateTime(dateFormattedFunction(f.getDateTime()));
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }

                    fncWarnings.setData(subFncWarnings);
                    return Result.success(fncWarnings);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return Result.success(fncWarnings);
    }

    @Override
    public Result findNewFncWarningSpare(String type, Integer position, Integer size,String sort) {
        size = position + size;
        Jedis jedis = null;
        FncWarningResults fncWarnings = new FncWarningResults();
        try {
            jedis = jedisPool.getResource();
            if (type.equals("all")){
                List<FncWarningResults> allFncResultsList = new ArrayList<>();
                Map<String, String> allFnc = jedis.hgetAll("SHAPE_WARRING");
                Set<Map.Entry<String, String>> allEntries = allFnc.entrySet();
                Iterator<Map.Entry<String, String>> iterator = allEntries.iterator();
                while (iterator.hasNext()){
                    Map.Entry<String, String> next = iterator.next();
                    FncWarningResults fnc = JSONObject.parseObject(next.getValue(), FncWarningResults.class);
                    allFncResultsList.add(fnc);
                }
                return Result.success(allFncResultsList);
            }else{
                List<String> result = jedis.hmget("SHAPE_WARRING", type);
                if (result.size() > 0 && result.get(0) != null){
                    fncWarnings = JSONObject.parseObject(result.get(0), FncWarningResults.class);
                    if (position > fncWarnings.getData().size()){
                        position = fncWarnings.getData().size();
                    }
                    if (size > fncWarnings.getData().size()){
                        size = fncWarnings.getData().size();
                    }
                    List<FncWarning> FncWarnings = fncWarnings.getData();
                    if("0".equals(sort)){
                        Collections.sort(FncWarnings, Comparator.reverseOrder());
                    }else {
                        Collections.sort(FncWarnings);
                    }
                    List<FncWarning> subFncWarnings = FncWarnings.subList(position,size);
                    for(FncWarning f:subFncWarnings){
                        try {
                            f.setDateTime(dateFormattedFunction(f.getDateTime()));
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                    fncWarnings.setData(subFncWarnings);
                    return Result.success(fncWarnings);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }

        return Result.success(fncWarnings);
    }

    @Override
    public Result findNewFncWarningSpareByData(String type, Integer position, Integer size, String date,String sort) {
         if(type.equals("all")){
             List<FncWarningResults>  allFncResultsList=ShapeHistoryTemplate.find( new Query(), FncWarningResults.class, date);
            return Result.success(allFncResultsList);
         }
         else {
             Query query = new Query();
             Criteria ca=new Criteria();
             ca.andOperator(Criteria.where("FncKey").is(type));
             query.addCriteria(ca);
            FncWarningResults  fncWarnings=ShapeHistoryTemplate.findOne(query, FncWarningResults.class, date);

            if(fncWarnings==null){
                return Result.success(new  FncWarningResults());
            }
             if (position > fncWarnings.getData().size()){
                 position = fncWarnings.getData().size();
             }
             int end=position+size;
             if (end > fncWarnings.getData().size()){
                 end = fncWarnings.getData().size();
             }
             List<FncWarning> FncWarnings = fncWarnings.getData();
             if("0".equals(sort)){
                 Collections.sort(FncWarnings, Comparator.reverseOrder());
             }else {
                 Collections.sort(FncWarnings);
             }
             List<FncWarning> subFncWarnings = FncWarnings.subList(position,end);
             for(FncWarning f:subFncWarnings){
                 try {
                     f.setDateTime(dateFormattedFunction(f.getDateTime()));
                 } catch (ParseException e) {
                     e.printStackTrace();
                 }
             }

             fncWarnings.setData(subFncWarnings);
             return  Result.success(fncWarnings);

         }
    }
    private  String dateFormattedFunction(String olddate) throws ParseException {
//        Date de=simpleDateFormat.parse(olddate);
//        return simpleDateFormat1.format(de);下一个版本更新内容
        return  olddate;
    }
}
